package com.snjx.netty.chat.netty;

import com.snjx.netty.chat.netty.handler.MyChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import org.springframework.beans.factory.annotation.Value;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName WebsocketServerChannelInitializer
 * @Author yunlong.zhang
 * @Date 2018/11/19
 * @since: JDK 1.8
 * @Description //TODO
 **/
public class WebsocketServerChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
       // HttpServerCodec：将请求和应答消息解码为HTTP消息
        pipeline.addLast("http-codec", new HttpServerCodec());
        // HttpObjectAggregator：将HTTP消息的多个部分合成一条完整的HTTP消息
        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
        // ChunkedWriteHandler：向客户端发送HTML5文件
        pipeline.addLast("http-chunked", new ChunkedWriteHandler());
        // 进行设置心跳检测
        pipeline.addLast(new IdleStateHandler(readerIdleTime, writerIdleTime, allIdleTime, TimeUnit.SECONDS));
        // 定义缓存数据量
        pipeline.addLast(new HttpObjectAggregator(1024*64));
        //websocket 服务器处理的协议，用于指定给客户端连接访问的路由 : /ws
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
        // 自定义的handler
        // socketChannel.pipeline().addLast(new LikePushHandler());
        // 配置通道处理  来进行业务处理
        //socketChannel.pipeline().addLast(new MyChannelHandler());
        // 换行分割解码器
        pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
        pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
        pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
        socketChannel.pipeline().addLast(new MyChannelHandler());
    }
    @Value("${netty.server.readerIdleTime}")
    public Integer readerIdleTime;

    @Value("${netty.server.writerIdleTime}")
    public Integer writerIdleTime;

    @Value("${netty.server.allIdleTime}")
    public Integer allIdleTime;
    public Integer getReaderIdleTime() {
        return readerIdleTime;
    }

    public void setReaderIdleTime(Integer readerIdleTime) {
        this.readerIdleTime = readerIdleTime;
    }
    public Integer getWriterIdleTime() {
        return writerIdleTime;
    }

    public void setWriterIdleTime(Integer writerIdleTime) {
        this.writerIdleTime = writerIdleTime;
    }

    public Integer getAllIdleTime() {
        return allIdleTime;
    }

    public void setAllIdleTime(Integer allIdleTime) {
        this.allIdleTime = allIdleTime;
    }
}
